package com.demo.spring.security.config.oauth2;

import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

@Configuration
@RequiredArgsConstructor(onConstructor=@__({@Autowired,@Lazy}))
public class CusAccessTokenConfig {
    /** JWT 签名字符串 */
    private final static String SIGNING_KEY = "javaboy";
    /** JWT信息转换器，将JWT和用户信息相互转换 */
    @Setter(onMethod=@__({@Autowired,@Qualifier("jwtAccessTokenConverter")}))
    private JwtAccessTokenConverter jwtAccessTokenConverter;

    /** 使用无存储 JwtTokenStore */
    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(jwtAccessTokenConverter);
    }

    /** JWT转换器，将JWT和用户信息相互转换 */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setSigningKey(SIGNING_KEY);
        return converter;
    }
}
